/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

public class PropertiesHelper {
    public static void addFieldProperties(String prefix, Properties properties, List list, PsiManager psiManager, Project project) {
        String destinationPackage = (String) properties.get("PACKAGE_NAME");
        StringBuffer imports = new StringBuffer();
        int fieldIndex = 1;
        StringBuffer fieldList = new StringBuffer();
        StringBuffer fieldSignatureList = new StringBuffer();
        for (Iterator iterator = list.iterator(); iterator.hasNext(); fieldIndex++) {
            ParameterInfo parameterInfo = (ParameterInfo) iterator.next();
            String pck = parameterInfo.getPackageName();
            if (pck != null && !pck.equals("java.lang") && !pck.equals(destinationPackage)) {
                String canonicalText = parameterInfo.getPsiType().getCanonicalText();
                PsiClass aClass = psiManager.findClass(canonicalText, GlobalSearchScope.allScope(project));
                if (aClass != null) {
                    imports.append("import ").append(canonicalText).append(";");
                }
            }
            System.out.println("parameterInfo = " + parameterInfo);

            properties.setProperty(prefix + "_" + (fieldIndex) + "_CLASS", parameterInfo.getFieldTypeString());
            properties.setProperty(prefix + "_" + (fieldIndex) + "_NAME", parameterInfo.getFieldName());
            properties.setProperty(prefix + "_" + (fieldIndex) + "_TESTNAME", parameterInfo.getTestFieldName());
            properties.setProperty(prefix + "_" + (fieldIndex) + "_GETTER", createGetter(parameterInfo.getFieldName()));
            if (fieldList.length() > 0) {
                fieldList.append(", ");
                fieldSignatureList.append(", ");
            }
            fieldList.append(parameterInfo.getFieldName());
            fieldSignatureList.append(parameterInfo.toString());
        }
        properties.setProperty(prefix + "_LIST", fieldList.toString());
        properties.setProperty(prefix + "_SIGNATURE", fieldSignatureList.toString());
        properties.setProperty(prefix + "_IMPORTS", imports.toString());

    }

    private static String createGetter(String fieldName) {
        if (fieldName == null) {
            return null;
        }
        if (fieldName.length() == 0) {
            return "";
        }
        return "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }


}
